package com.yu;

import java.util.concurrent.TimeUnit;

public class DealLock {
    //必然造成死锁的例子
    public static void main(String[] args) {
        Object a = new Object();
        Object b = new Object();

        new Thread(()->{
            synchronized (a){
                System.out.println(Thread.currentThread().getName() + "获取了a资源,准备获取b资源");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b){
                }
            }
        },"线程A").start();

        new Thread(()->{
            synchronized (b){
                System.out.println(Thread.currentThread().getName() + "获取了b资源,准备获取A资源");
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a){

                }
            }
        },"线程B").start();
    }
}
